import { Component } from '@angular/core';
import { Student } from './model/student';
import { StudentService } from './service/student.service';
import { StudentVo } from './vo/student-vo';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})




export class AppComponent {
  //变量定义
testvalue:String;
numbervalue:Number;
  title = 'app';

  student:Student;
  sl:Array<Student>;

  studentvo:StudentVo;

  constructor(private studentservice:StudentService){

  }




//初始化方法
ngOnInit() {
  this.testvalue="今天天气很好";
  this.numbervalue=25+36;
  this.studentvo=new StudentVo();

  this.student=new Student();
  this.sl=new Array();
  this.loadStudentList();

  //测试部分
  // this.student.name="测试姓名";
  // this.student.sex="男";
  // this.student.birthday=new Date().getTime();
  this.studentservice.getAnObject()
  .then((data:any)=>{
    this.student=data;
  }) 
}

//添加事件
add(){
  console.dir(this.studentvo);
  this.studentservice.addStudentInfo(this.studentvo)
  .then((data:any)=>{
    //完成请求事件后
    this.loadStudentList();
    
  })
}

//载入student列表
loadStudentList(){

  this.studentservice.getStudentList()
  .then((data:any)=>{
   this.sl=data;
  })

}

//通过获取主键来获取单个student对象的主键
getToUpdateStudent(id:string){

  this.studentservice.getSingleStudentById(id)
  .then((data:Student)=>{
  this.studentvo=new StudentVo();
  this.studentvo.studentid=data.studentid;
  this.studentvo.xh=data.stunum;
  this.studentvo.xm=data.name;
  
  })
  
  }

  //删除单个对象
  deleteStudent(id:string){
    this.studentservice.deleteStudent(id)
    .then((flag:boolean)=>{
      if(flag==true){
        this.loadStudentList();
      }
      else{
        alert("操作异常，请刷新后重试");
      }
    })
  }


}



